package com.pocketsweet.ui.uilib.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.pocketsweet.R;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.Profile;
import com.pocketsweet.utils.StringUtils;

/**
 * 
 * @author Kam
 * 
 */
public class LikePeopleAdapter extends BaseListAdapter<MLUser> {
	public LikePeopleAdapter(Context ctx) {
		super(ctx);
	}

	public LikePeopleAdapter(Context ctx, List<MLUser> datas) {
		super(ctx, datas);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_like_poeple, parent,
					false);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.refreshView(datas.get(position), position);
		return convertView;
	}

	private class ViewHolder implements View.OnClickListener {
		ImageView imgAvatar;
		TextView tvuserName;
		TextView tvUserAge;
		TextView tvFrom;
		TextView tvStar;
		MLUser item;
		private View parent;

		private ViewHolder(View parent) {
			this.parent = parent;
			initView(parent);
		}

		public void initView(View parent) {
			imgAvatar = (ImageView) parent.findViewById(R.id.imgAvatar);
			tvuserName = (TextView) parent.findViewById(R.id.tvuserName);
			tvUserAge = (TextView) parent.findViewById(R.id.tvUserAge);
			tvFrom = (TextView) parent.findViewById(R.id.tvFrom);
			tvStar = (TextView) parent.findViewById(R.id.tvStar);
		}

		public void refreshView(MLUser item, int i) {
			this.item = item;
			String location = null;
			String avatar = item.getAvatarUrl();
			if (avatar != null && !avatar.equals("")) {
				UserService.displaySquare(avatar, imgAvatar);
			} else {
				imgAvatar.setImageResource(R.drawable.default_user_avatar);
			}
			// 设置姓名
			tvuserName.setText(item.getNickname());
			// 设置年龄
			if (!StringUtils.isEmpty(String.valueOf(item.getAgeNum()))) {
				tvUserAge.setText(String.valueOf(item.getAgeNum()));
			}
			// 设置性别颜色
			// GradientDrawable d = (GradientDrawable) ctx.getResources()
			// .getDrawable(R.drawable.shape_rectangle_bule);
			if (item.getGender() == 1) {
				tvUserAge
						.setBackgroundResource(R.drawable.shape_rectangle_bule);

				// d.setColor(ctx.getResources().getColor(
				// R.color.border_bottom_blue));// 设置颜色
			} else if (item.getGender() == 2) {
				tvUserAge.setBackgroundResource(R.drawable.shape_rectangle_red);
			}
			// 设置星座
			tvStar.setText(item.getConstellation());
			// 设置地理位置
			location = item.getLocationCity();
			if (StringUtils.isEmpty(location)) {
				location = item.getLocationProvince();
			}
			tvFrom.setText(location);
			parent.setOnClickListener(this);
		}

		@Override
		public void onClick(View v) {
			// 查看用户资料
			Intent intent = new Intent(ctx, Profile.class);
			intent.putExtra("userId", item.getObjectId());
			intent.putExtra("nickname", item.getNickname());
			ctx.startActivity(intent);

		}

	}
}
